from selenium.common import *
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

from common.report_add_img import add_img_2_report


class Base:

    def find_element_new(self,driver,locator_type,locator_value,timeout=10):
        try:
            element=WebDriverWait(driver,timeout).until(
                EC.presence_of_element_located((locator_type,locator_value))
            )                                                   #显示等待
            return element
        except(TimeoutException,NoSuchElementException):
            print(f"元素未找到：{locator_type},{locator_value}")
            return None

    def find_elements_new(self,driver,locator_type,locator_value,timeout=10):
        try:
            elements=WebDriverWait(driver,timeout).until(
                EC.presence_of_all_elements_located((locator_type,locator_value))
            )
            return elements
        except(TimeoutException,NoSuchElementException):
            print(f"元素未找到：{locator_type},{locator_value}")
            return None

    def send_keys_new(self,driver,locator_type,locator_value,value,timeout=10):
        try:
            element=self.find_element_new(driver,locator_type,locator_value,timeout)
            element.clear()
            element.send_keys(value)

        except Exception as e:
            print(f"在元素{locator_type,locator_value}中输入内容失败：{str(e)}")


    def click_new(self,driver,locator_type,locator_value,timeout=10):
        try:
            element=self.find_element_new(driver,locator_type,locator_value,timeout)
            element.click()

        except Exception as e:
            print(f"点击元素{locator_type,locator_value}失败:{str(e)}")


    def get_text(self, driver, locator_type, locator_value):
        try:
            element = self.find_element_new(driver, locator_type, locator_value)
            element_text = element.text
            print(f"获取到的文本：{element_text}")
            return element_text
        except Exception as e:
            print(f"元素text获取失败，错误信息为：{str(e)}")


class BaseAssert:
    def assert_equal_screen_shot(self,driver,exceptor,step_name):
        if not isinstance(exceptor,tuple):
            print('exceptor参数错误，必须传元组类型')

        else:
            print(f"正在断言: {exceptor[0]} 是否等于 {exceptor[1]}")
            try:
                assert exceptor[0] == exceptor[1]
                add_img_2_report(driver, step_name=step_name)
            except Exception as e:
                print(f"断言失败: {str(e)}")
                add_img_2_report(driver, step_name=step_name)
                raise e



